/**
 * Created by Administrator on 2018/5/31.
 */


var points = [
    {'name':'宝兴县', 'pos':[102.82058,30.388173]},
    {'name':'天全县', 'pos':[102.778663,30.063344]},
    {'name':'荥经县', 'pos':[102.853267,29.799434]},
    {'name':'芦山县', 'pos':[102.939033,30.148236]},
    {'name':'汉源县', 'pos':[102.657688,29.349799]},
    {'name':'石棉县', 'pos':[102.365654,29.234244]},
    {'name':'名山区', 'pos':[103.115734,30.0759]},
    {'name':'雨城区', 'pos':[103.039693,30.011103]}

];
var citys = [["甘孜藏族自治州",[99.9207,31.0803]],["阿坝藏族羌族自治州",[102.4805,32.4536]],["凉山彝族自治州",[101.9641,27.6746]],["绵阳市",[104.7327,31.8713]],["达州市",[107.6111,31.333]],["广元市",[105.6885,32.2284]],
    //["雅安市",[102.6672,29.8938]],
    ["宜宾市",[104.6558,28.548]],["乐山市",[103.5791,29.1742]],["南充市",[106.2048,31.1517]],["巴中市",[107.0618,31.9977]],["泸州市",[105.4578,28.493]],["成都市",[103.9526,30.7617]],["资阳市",[104.9744,30.1575]],["攀枝花市",[101.6895,26.7133]],["眉山市",[103.8098,30.0146]],["广安市",[106.6333,30.4376]],["德阳市",[104.48,31.1133]],["内江市",[104.8535,29.6136]],["遂宁市",[105.5347,30.6683]],["自贡市",[104.6667,29.2786]]];

function drawMapFlow(id, model){
    var pts = [], pt2 = [], pt3 = [];
    for(var i=0; i<citys.length; i++){
        if(Math.random()<0.4 || citys[i][0]=="雅安市")continue;
        pts.push({ name:citys[i][0], value:citys[i][1].concat(parseInt(Math.random() * 20)) });
    }
    pt3.push({ name:"雅安地区", value:[102.6672,29.8938, 5] });
    points.forEach(function(d){
        pt2.push({ name: d.name, value: d.pos.concat(parseInt(Math.random() * 10)) });
    });
    var lines = pts.map(function(d) {
        return {
            fromName: d.name,
            toName: "雅安地区",
            coords: [d.value, [102.6672,29.8938]],
            value: 1
        };
    });
    cout(lines);
    var items = points.map(function(e){ return {name: e.name, value:d(60, 150)}; });
    var series = [
        {
            type: 'lines',
            zlevel: 2,
            //symbol: ['none', 'arrow'],
            //symbolSize: 3,
            effect: {
                show: true,
                period: 4,
                trailLength: 0.02,
                symbol: 'arrow',
                symbolSize: 5,
            },
            lineStyle: {
                normal: {
                    width: 1,
                    opacity: 0.6,
                    curveness: 0.2,
                    color:'aqua'
                }
            },
            data: lines
        },
        //// 雅安辖区外
        {
            name: "干部流动",
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: { //涟漪特效
                period: 4, //动画时间，值越小速度越快
                brushType: 'stroke', //波纹绘制方式 stroke, fill
                scale: 4 //波纹圆环最大限制，值越大波纹越大
            },
            label: {
                normal: {
                    show: true,
                    position: 'right', //显示位置
                    offset: [5, 0], //偏移设置
                    formatter: '{b}' //圆环显示文字
                },
                emphasis: {
                    show: true
                }
            },
            symbol: 'circle',
            symbolSize: function(val) {
                return 4 + val[2] / 2;
            },
            itemStyle: {
                normal: {
                    show: false,
                    color: 'aqua'
                }
            },
            data: pts
        },
        //// 雅安县级
        {
            name:'干部分布',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                period: 4,
                brushType: 'stroke',
                scale: 4
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    //			                offset:[5, 0],

                    color: '#00ffff',
                    formatter: '{b}',
                    textStyle: {
                        color: "#00ffff"
                    }
                },
                emphasis: {
                    show: true
                }
            },
            symbol: 'circle',
            symbolSize: function(val) {
                return 2 + val[2] / 2;
            },
            itemStyle: {
                normal: {
                    show: true,
                    color: 'lime'
                }
            },
            data: pt2
        },
        //// 雅安
        {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                period: 4,
                brushType: 'stroke',
                scale: 4
            },
            label: {
                normal: {
                    show: true,
                    position: 'left',
                    color: 'orange',
                    formatter: '{b}',
                    textStyle: {
                        color: "orange"
                    }
                },
                emphasis: {
                    show: true
                }
            },
            symbol: 'circle',
            symbolSize: function(val) {
                return 2 + val[2] / 2;
            },
            itemStyle: {
                normal: {
                    show: true,
                    color: 'orange'
                }
            },
            data: pt3
        }
    ];
    var map = {
        type: 'map',
        mapType: '雅安',
        label: {
            normal: {
                show: false
            },
            emphasis: {
                textStyle: {
                    color: '#fff'
                }
            }
        },
        itemStyle: {
            normal: {
                borderColor: '#389BB7',
                areaColor: '#fff',
            },
            emphasis: {
                areaColor: '#389BB7',
                borderWidth: 0
            }
        },
        data: items,
        animation: true
    };


    var option1 = {
        backgroundColor: '#191e48', // 0f0c29   191970
        title : { text: '干部籍贯分布流动', left: 'left', textStyle:{color:'#fff', fontSize:13}, top:20 }, tooltip : { trigger: 'item' },
        legend: {
            orient: 'vertical', top: 20, left: 'right',
            data:["干部流动", "干部分布"], textStyle: {color: '#fff' }
        },
        geo: {
            map: '四川', label: { normal: { show: false }, emphasis:{ textStyle:{ color: '#00FFFF'} } }, zoom: 6.5, roam: true,
            center:[103.035552,30.218851],
            itemStyle: {
                normal: {
                    color: '#262C50',//'rgba(51, 69, 89, .5)',
                    borderColor: 'rgba(100,149,237,1)'
                },
                emphasis: {
                    color: 'rgba(37, 43, 61, .5)'
                }
            },
            regions:[{name:'省外', itemStyle:{normal:{color:'#111542'}}}]  //#191970
        },
        series: series
    };
    var option2 = {
        backgroundColor: '#191970', // 0f0c29
        title : { text: '干部地理分布', left: 'left', textStyle:{color:'#fff', fontSize:13} }, tooltip : { trigger: 'item' },
        visualMap: {
            min: 0,
            //max: 100,
            left: 'left',
            top: 'bottom',
            text: ['高', '低'],
            calculable: true,
            inRange: {
                color: ['#ffffff', '#E0DAFF', '#ADBFFF', '#9CB4FF', '#6A9DFF', '#3889FF']
            },
            textStyle:{ color:'#fff' }
        },

        geo: {
            map: '雅安', label: { normal: { show: false }, emphasis:{ textStyle:{ color: '#00FFFF'} } }, roam: true,
            //center:[103.035552,30.218851],
            itemStyle: {
                normal: {
                    color: 'rgba(51, 69, 89, .5)',
                    borderColor: 'rgba(100,149,237,1)'
                },
                emphasis: {
                    color: 'rgba(37, 43, 61, .5)'
                }
            },
            //regions:[{name:'省外', itemStyle:{normal:{color:'#191970'}}}]
        },
        series:[map, series[2]]
    };

    if(model){
        var e = Init3(option2, id);
        e._zr.on('click', function(p){
            if(p.target==undefined)drawMapFlow(id, 0);
        });
    }else{
        var e = Init3(option1, id);
        e.on('click', function(d){
            if(d.name!='雅安市')return;
            drawMapFlow(id, 1);
        });
    }


}

function drawBar(id){
    var keys = points.map(function(d){ return d.name; });
    var v1 = keys.map(function(){
        return 10 + parseInt(Math.random() * 30);
    });
    var v2 = keys.map(function(){
        return 5 + parseInt(Math.random() * 10);
    });
    v1.sort();

    var option = {
        backgroundColor: '#1B204A',
        title:{ text:'各区县干部数量分布', textStyle:{ color:'#26ECED', fontSize:14 }, top:15 },
        legend: {
            top: 50,
            textStyle:{
                color:'#fff',
            },
            data: ['本地', '异地']
        },
        grid: {
            top: '120px',
            left: '3%',
            right: '4%',
            bottom: '10%',
            containLabel: true
        },
        tooltip: {
            show:"true",
            trigger: 'axis',
            axisPointer: {      // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        xAxis:  {
            name:'(干部数)',
            type: 'value',
            position: 'top',
            axisTick : {show: false},
            axisLine: {
                show: true,
                lineStyle:{
                    color:'#fff',
                }
            },
            splitLine: {
                show: false
            },
        },
        yAxis: [
            {
                name:'区县             ',
                nameTextStyle:{ align:'left' },
                type: 'category',
                axisTick : {show: false},
                axisLine: {
                    show: true,
                    lineStyle:{
                        color:'#fff',
                    }
                },
                data: keys
            },
            {
                type: 'category',
                axisLine: {show:false},
                axisTick: {show:false},
                axisLabel: {show:false},
                splitArea: {show:false},
                splitLine: {show:false},
                data: keys
            },
        ],
        series: [
            {
                name: '本地',
                type: 'bar',
                yAxisIndex:1,
                itemStyle:{
                    normal: {
                        show: true,
                        color: '#277ace',
                        barBorderRadius:50,
                        borderWidth:0,
                        borderColor:'#333',
                    }
                },
                barMaxWidth:25,
                barGap:'0%',
                barCategoryGap:'20%',
                data: v1
            },
            {
                name: '异地',
                type: 'bar',
                itemStyle:{
                    normal: {
                        show: true,
                        color: '#00FF7F',  //#5de3e1  00FA9A
                        barBorderRadius:50,
                        borderWidth:0,
                        borderColor:'#333',
                        opacity:0.9,
                    }
                },
                barMaxWidth:25,
                barGap:'0%',
                barCategoryGap:'20%',
                data: v2
            }

        ]
    };

    var e = Init3(option, id);
    e.on('click', function(p){
        setTable(p.name);
        //cout(p.name);

    });
}


function setTable(region){
    var html = "", count = 6 + parseInt(Math.random() * 6), dts = [];
    var names = ['赵', '钱', '孙', '李', '张', '周', '吴', '郑', '王', '冯'];
    for(var i=0; i<count; i++){
        var name = names[d(0, names.length)] + "*";
        var work = "2";
        var home = ((i+1)%3==0) ? citys[parseInt(Math.random() * citys.length)][0] : points[parseInt(Math.random() * points.length)].name ;
        html+='<tr class="error"><td>'+ name +'</td><td>'+ region +'</td><td>'+ work +'</td><td>'+ home +'</td></tr>';
    }

    $("#tbody1").html(html);
    $("#node").text(region + " 干部人员详情");
}

